/* globals.cpp - source for Global.
 */

#include "common.h"

Global::OptionBag   Global::Options =
    {
    -1,                 // ExpectCode
    -1,                 // ExpectLine
    0,                  // TabStop
    0,                  // VerboseFlag
    false,              // TokenizeFlag
    NULL,               // ShowFirst
    NULL,               // ShowFollow
    NULL,               // StyleFile
    };

#if 0
int          Global::ExpectCode     = -1;
int          Global::ExpectLine     = -1;
int          Global::TabStop;
bool         Global::DumpFlag;
bool         Global::VerboseFlag;
bool         Global::TokenizeFlag;
const char*  Global::InputFile;
const char*  Global::ShowFirst;
const char*  Global::ShowFollow;
const char*  Global::StyleFile;

#endif

/* ParseArgs() - parse command-line arguments.
 *
 */
void Global::ParseArgs(int ArgCount, char** Args, Global::OptionBag* Temp)
    {
    int         iArg;
    const char* Arg;
    
    if(Temp == NULL)
        Temp    = &Global::Options;

    if(ArgCount < 2)
        Usage("You must supply an input filename.\n");

    for(iArg = 1; iArg < ArgCount; ++iArg)
        {
        Arg     = Args[iArg];
        if(Arg[0] == '-')
            {
            int     Char;

            ++Arg;
            while((Char = *Arg++) != '\0')switch(Char)
                {
                case    'c' :
                    Temp->ExpectCode   = GetIntArg(Char, Arg);
                    Arg = Arg + strlen(Arg);
                    break;
                case    'd' :   Temp->DumpFlag    = true;
                    break;
                case    'f' :
                    if(Arg[-2] != '-')
                        Usage("'%s': -f<name> cannot be combined with other options.\n", Args[iArg]);
                    else
                        {
                        Temp->ShowFirst   = Arg;
                        if(!ISALPHA(Temp->ShowFirst[0]))
                            Usage("Illegal character after '-f'. Use -fname to show FIRST set of non-terminal 'name'\n");
                        Arg        += strlen(Arg);
                        }
                    break;
                case    's' :
                    if(Arg[-2] != '-')
                        Usage("'%s': -s<name> cannot be combined with other options.\n", Args[iArg]);
                    else
                        {
                        Temp->StyleFile   = Arg;
                        Arg        += strlen(Arg);
                        }
                    break;
                case    'w' :
                    if(Arg[-2] != '-')
                        Usage("'%s': -w<name> cannot be combined with other options.\n", Args[iArg]);
                    else
                        {
                        Temp->ShowFollow   = Arg;
                        if(!ISALPHA(Temp->ShowFollow[0]))
                            Usage("Illegal character after '-w'. Use -wname to show FOLLOW set of non-terminal 'name'\n");
                        Arg        += strlen(Arg);
                        }
                    break;
                case    'k' :   Temp->TokenizeFlag        = true;
                    break;
                case    'v' :   ++Temp->VerboseFlag;
                    break;
                case    'l' :
                    Temp->ExpectLine   = GetIntArg(Char, Arg);
                    Arg = Arg + strlen(Arg);
                    break;
                case    't' :
                    Temp->TabStop     = GetIntArg(Char, Arg);
                    Arg = Arg + strlen(Arg);
                    break;
                default:
                    Usage("'%c': Unknown option.\n", Char);
                }
            }
        else if(Temp->InputFile == NULL)
            Temp->InputFile   = Arg;
        else
            Usage("'%s': only one input file allowed.\n", Arg);
        }
    if(Temp->InputFile == NULL)
        Usage("Missing input file.\n");
    ::Dump("ParseArgs() returns\n");
    }

int     Global::GetIntArg(int Char, const char* Arg)
    {
    if(!ISDIGIT(*Arg))
        ErrorExit(ERROR_COMMANDLINE, "Expecting integer after -%c.\n", Char);
    return atoi(Arg);
    }

